<template>
    <div class="app-container">
        <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="行政班级id" prop="meataclassId">
          <el-input v-model="queryParams.meataclassId" placeholder="请输入行政班级id" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="queryParams.studentNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="queryParams.realName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="queryParams.gender" placeholder="请输入性别" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="queryParams.grade" placeholder="请输入年级" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
   -->
        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['niuyabo:student:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['niuyabo:student:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['niuyabo:student:remove']">删除</el-button>
        </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['score:student:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="学生id" align="center" prop="studentId" />
            <el-table-column label="学号" align="center" prop="studentNo" />
            <el-table-column label="姓名" align="center" prop="realName" />
            <!-- <el-table-column label="行政班级id" align="center" prop="meataclassId" /> -->
            <el-table-column label="行政班级" align="center" prop="metaclass.metaclassName" />
            <el-table-column label="课程" align="center" prop="courseclassMetaclass.courseclass.course.courseName" />
            <!-- <el-table-column label="状态" align="center" prop="status" /> -->
            <el-table-column label="性别" align="center" prop="gender" />
            <el-table-column label="年级" align="center" prop="grade" />
            <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->

            <el-table-column label="平时成绩" align="center" prop="courseclassMetaclass.courseclassStudent.pingshiScore" />
            <el-table-column label="期末成绩" align="center" prop="courseclassMetaclass.courseclassStudent.qimoScore" />
            <el-table-column label="总评成绩" align="center" prop="courseclassMetaclass.courseclassStudent.zongpingScore" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >录入成绩</el-button>
                        <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['niuyabo:CourseclassStudent:edit']">录入成绩</el-button> -->
                </template>
            </el-table-column>
            <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['niuyabo:student:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['niuyabo:student:remove']">删除</el-button>
          </template>
        </el-table-column> -->
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />


        <!-- 添加或修改学生上课对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <!-- <el-form-item label="学生id" prop="student_id">
                    <el-input v-model="form.student_id" placeholder="请输入平时成绩" />
                </el-form-item>
                <el-form-item label="上课班级id" prop="courseclass_id">
                    <el-input v-model="form.courseclass_id" placeholder="请输入平时成绩" />
                </el-form-item> -->
                <el-form-item label="平时成绩" prop="pingshiScore">
                    <el-input v-model="form.pingshiScore" placeholder="请输入平时成绩" />
                </el-form-item>
                <el-form-item label="期末成绩" prop="qimoScore">
                    <el-input v-model="form.qimoScore" placeholder="请输入期末成绩" />
                </el-form-item>
                <el-form-item label="总评成绩" prop="zongpingScore">
                    <el-input v-model="form.zongpingScore" placeholder="请输入总评成绩" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改学生管理对话框 -->
        <!-- <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="学生用户" prop="userId">
            <el-select v-model="form.userId" filterable placeholder="请绑定学生用户" style="width: 150px">
              <el-option v-for="item in userList" :key="item.userId" :label="item.userName" :value="item.userId"
                :disabled="item.status == 1"></el-option>
            </el-select>
            用户id：<el-input v-model="form.userId" disabled placeholder="请输入用户id" style="width: 100px" />
          </el-form-item>
  
          <el-form-item label="行政班级" prop="meataclassId">
            <el-select v-model="form.meataclassId" filterable placeholder="请选择行政班级">
              <el-option v-for="item in metaclassList" :key="item.meataclassId" :label="item.metaclassName"
                :value="item.meataclassId" :disabled="item.status == 1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学号" prop="studentNo">
            <el-input v-model="form.studentNo" placeholder="请输入学号" />
          </el-form-item>
          <el-form-item label="姓名" prop="realName">
            <el-input v-model="form.realName" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-input v-model="form.gender" placeholder="请输入性别" />
          </el-form-item>
          <el-form-item label="年级" prop="grade">
            <el-input v-model="form.grade" placeholder="请输入年级" />
          </el-form-item>
  
          <el-divider content-position="center">学生上课信息</el-divider>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCourseclassStudent">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini"
                @click="handleDeleteCourseclassStudent">删除</el-button>
            </el-col>
          </el-row>
          <el-table :data="courseclassStudentList" :row-class-name="rowCourseclassStudentIndex"
            @selection-change="handleCourseclassStudentSelectionChange" ref="courseclassStudent">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50" />
  
  
            <el-table-column label="上课班级" prop="courseclassId" width="150px">
              <template slot-scope="scope">
                <el-select v-model="scope.row.courseclassId" filterable placeholder="请选择上课班级">
                  <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.courseclassNo"
                    :value="item.courseclassId" :disabled="item.status == 1"></el-option>
                </el-select>
              </template>
            </el-table-column>
  
            <el-table-column label="上课班级名" prop="course.courseName" width="120px">
              <template slot-scope="scope">
                <el-select v-model="scope.row.courseclassId" filterable placeholder="请选择上课班级名">
                  <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.course.courseName"
                    :value="item.courseclassId" :disabled="item.status == 1"></el-option>
                </el-select>
              </template>
            </el-table-column>
  
            <el-table-column label="平时成绩" prop="pingshiScore" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.pingshiScore" placeholder="请输入平时成绩" />
              </template>
            </el-table-column>
            <el-table-column label="期末成绩" prop="qimoScore" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.qimoScore" placeholder="请输入期末成绩" />
              </template>
            </el-table-column>
            <el-table-column label="总评成绩" prop="zongpingScore" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.zongpingScore" placeholder="请输入总评成绩" />
              </template>
            </el-table-column>
          </el-table>
  
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog> -->
    </div>
</template>
  
<script>
// import { listStudent, getStudent, delStudent, addStudent, updateStudent } from "@/api/niuyabo/student";
import { listCourseclassStudent, getCourseclassStudent, delCourseclassStudent, addCourseclassStudent, updateCourseclassStudent } from "@/api/score/CourseclassStudent";

//   import { listMetaclass } from "@/api/niuyabo/metaclass";
//   import { allocatedUserList } from "@/api/system/role";
//   import { listCourseclass } from "@/api/niuyabo/courseclass";
import { getCourseclassTeacherAndStudent } from "@/api/score/CourseclassTeacher";

export default {
    name: "Students",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 子表选中数据
            checkedCourseclassStudent: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 学生管理表格数据
            studentList: [],
            // 学生上课表格数据
            courseclassStudentList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 班级管理表格数据
            metaclassList: [],
            // 学生用户表格数据
            userList: [],
            // 课程班级表格数据
            courseclassList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                meataclassId: null,
                status: null,
                studentNo: null,
                realName: null,
                gender: null,
                grade: null,
                userId: null,
                courseclassId: 0
            },
            queryParams1: {
                studentId: null,
                courseclassId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    // created() {

    //   this.getList();
    // },
    created() {
        const courseclassId = this.$route.params && this.$route.params.courseclassId;
        if (courseclassId) {
            this.queryParams.courseclassId = courseclassId;
            this.getList();
        }

    },
    methods: {
        /** 查询学生管理列表 */
        getList() {
            this.loading = true;
            // listStudent(this.queryParams).then(response => {
            //   this.studentList = response.rows;
            //   this.total = response.total;
            //   this.loading = false;
            // });
            getCourseclassTeacherAndStudent(this.queryParams.courseclassId).then(response => {
                // this.form = response.data;
                this.studentList = response.rows;
                console.log(this.studentList);
                this.total = response.total;
                this.loading = false;
                // this.open = true;
                // this.title = "查看学生列表";
            });
            // /** 查询班级管理列表 */
            // listMetaclass().then(response => {
            //   this.metaclassList = response.rows;
            //   // this.total = response.total;
            //   // this.loading = false;
            // });
            // /** 查询授权教师用户列表 */
            // allocatedUserList(this.queryParams).then(response => {
            //   this.userList = response.rows;
            //   // this.total = response.total;
            //   // this.loading = false;
            // });
            // /** 查询课程班级列表 */
            // listCourseclass().then(response => {
            //   this.courseclassList = response.rows;
            // });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                studentId: null,
                meataclassId: null,
                status: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                studentNo: null,
                realName: null,
                gender: null,
                grade: null,
                userId: null
            };
            this.courseclassStudentList = [];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.studentId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加学生管理";
        },
        // /** 修改按钮操作 */
        // handleUpdate(row) {
        //     this.reset();
        //     const studentId = row.studentId || this.ids
        //     getStudent(studentId).then(response => {
        //         this.form = response.data;
        //         this.courseclassStudentList = response.data.courseclassStudentList;
        //         this.open = true;
        //         this.title = "修改学生管理";
        //     });
        // },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const studentId = row.studentId || this.ids
            this.queryParams1.studentId = studentId;
            const courseclassId = this.$route.params && this.$route.params.courseclassId;
            this.queryParams1.courseclassId = courseclassId;
            listCourseclassStudent(this.queryParams1).then(response => {
                // this.CourseclassStudentList = response.rows;
                // this.total = response.total;
                // this.loading = false;
                if (response.rows.length > 0) {
                    this.form = response.rows[0];
                }
                // this.courseclassStudentList = response.rows;
                this.open = true;
                this.title = "填写学生成绩";
            });

            // getStudent(studentId).then(response => {
            //     this.form = response.data;
            //     this.courseclassStudentList = response.data.courseclassStudentList;
            //     this.open = true;
            //     this.title = "填写学生成绩";
            // });
        },
        // /** 提交按钮 */
        // submitForm() {
        //     this.$refs["form"].validate(valid => {
        //         if (valid) {
        //             // this.form.courseclassStudentList = this.courseclassStudentList;
        //             if (this.form.studentId != null) {
        //                 updateStudent(this.form).then(response => {
        //                     this.$modal.msgSuccess("修改成功");
        //                     this.open = false;
        //                     this.getList();
        //                 });
        //             } else {
        //                 addStudent(this.form).then(response => {
        //                     this.$modal.msgSuccess("新增成功");
        //                     this.open = false;
        //                     this.getList();
        //                 });
        //             }
        //         }
        //     });
        // },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.studentId != null) {
                        updateCourseclassStudent(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else if (this.form.pingshiScore) {

                        this.form.studentId = this.queryParams1.studentId;
                        const courseclassId = this.$route.params && this.$route.params.courseclassId;
                        this.form.courseclassId = courseclassId;

                        addCourseclassStudent(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const studentIds = row.studentId || this.ids;
            this.$modal.confirm('是否确认删除学生管理编号为"' + studentIds + '"的数据项？').then(function () {
                return delStudent(studentIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 学生上课序号 */
        rowCourseclassStudentIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 学生上课添加按钮操作 */
        handleAddCourseclassStudent() {
            let obj = {};
            obj.pingshiScore = "";
            obj.qimoScore = "";
            obj.zongpingScore = "";
            this.courseclassStudentList.push(obj);
        },
        /** 学生上课删除按钮操作 */
        handleDeleteCourseclassStudent() {
            if (this.checkedCourseclassStudent.length == 0) {
                this.$modal.msgError("请先选择要删除的学生上课数据");
            } else {
                const courseclassStudentList = this.courseclassStudentList;
                const checkedCourseclassStudent = this.checkedCourseclassStudent;
                this.courseclassStudentList = courseclassStudentList.filter(function (item) {
                    return checkedCourseclassStudent.indexOf(item.index) == -1
                });
            }
        },
        /** 复选框选中数据 */
        handleCourseclassStudentSelectionChange(selection) {
            this.checkedCourseclassStudent = selection.map(item => item.index)
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('score/student/export', {
                ...this.queryParams
            }, `student_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
  